package com.tourscool.springboot.starter.inquiry;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tourscool.springboot.starter.inquiry.core.ControllerInterfaceUtils;
import com.tourscool.springboot.starter.inquiry.core.RequestMappingUtils;
import com.tourscool.springboot.starter.inquiry.core.RequestMethodItem;
import com.tourscool.springboot.starter.inquiry.utils.HttpRequestUtils;
import com.tourscool.springboot.starter.inquiry.utils.NetworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Cedric
 * @date 2020/11/03 13:27
 * @description 类功能描述
 */
@Slf4j
@Component
public class InterfaceReportHandler implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    private final static String STATUS = "status";
    private final static Integer STATUS_OK = 200;
    @Value("${service.inquiry.upload.url:http://localhost:11086/push}")
    private String uploadCenterUrl;
    private final static String LOCAL_HOST = "localhost";

    @PostConstruct
    public void scanAndReport() {
        if (uploadCenterUrl.contains(LOCAL_HOST)) {
            return;
        }
        boolean canAccess = HttpRequestUtils.option(uploadCenterUrl);
        if (!canAccess) {
            log.warn("客户端{}无法访问中心服务器", NetworkUtils.getIp());
            return;
        }
        List<RequestMethodItem> controllerMethodList = ControllerInterfaceUtils.getControllerMethodList(getApplicationContext());
        List<RequestMethodItem> requestMappingMethodList = RequestMappingUtils.getRequestMappingMethodList(getApplicationContext());
        List<RequestMethodItem> mergeRequestMethodList = new LinkedList<>();
        mergeRequestMethodList.addAll(controllerMethodList);
        mergeRequestMethodList.addAll(requestMappingMethodList);
        try {
            String result = HttpRequestUtils.post(uploadCenterUrl, new ObjectMapper().writeValueAsString(mergeRequestMethodList));
            if (StringUtils.isNotEmpty(result)) {
                if (Objects.equals(result, String.valueOf(true)) || Objects.equals(result, String.valueOf(false))) {
                    return;
                }
                Map resultMap = new ObjectMapper().readValue(result.getBytes(), Map.class);
                if (!Objects.equals(MapUtils.getIntValue(resultMap, STATUS), STATUS_OK)) {
                    log.info("客户端{}的上报未得到正确响应，请检查上传地址和参数是否正确", NetworkUtils.getIp());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    private ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }
}
